#!/bin/bash
# 系统配置脚本，在arch-chroot后执行

# 自行设定
ESP=/dev/sda1 # EFI 分区
USERNAME="rabbit" # 设置新的用户名
HOSTNAME="kua-arch" # 设置主机名
SWAPFILE_SIZE='1G' # 设置swap文件大小

source ./applist

# 预先要安装的软件
pacman -S --noconfirm --needed sudo wget

system_configure(){
    # 添加新的用户
    useradd -m -g users -s /bin/bash "$USERNAME" # 添加新的用户
    sed "/root ALL=(ALL) ALL/i"$USERNAME" ALL=(ALL) ALL" -i /etc/sudoers
    sed '/# %wheel ALL=(ALL) ALL/{s/# //}' -i /etc/sudoers
    gpasswd -a "$USERNAME" wheel # 新的用户添加到wheel组
    gpasswd -a "$USERNAME" games # 新的用户添加到game组
    gpasswd -a "$USERNAME" storage # 新的用户添加到storage组(挂载设备免输密码)

    # 更新hosts
    cp /etc/hosts /etc/hosts.bak # 备份原hosts
    sed "/localhost/s/$/\t${HOSTNAME}/g" -i /etc/hosts

    # 设置主机名
    echo "$HOSTNAME" > /etc/hostname

    # 添加 [archlinuxcn] 源
    cat << EOF >> /etc/pacman.conf
#Netease (网易源)
[archlinuxcn]
SigLevel = Optional TrustedOnly
Server = http://mirrors.163.com/archlinux-cn/\$arch
EOF
pacman -Syy --noconfirm archlinuxcn-keyring

sed '/#Color/{s/#//}' -i /etc/pacman.conf # pacman 彩色输出

# 设置时区与时间
timedatectl set-ntp true
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-timezone Asia/Shanghai # 时区设置为 上海
hwclock --systohc --utc # 从当前系统时间设置硬件时钟并保持为 UTC 时间
#timedatectl set-local-rtc 0 # 硬件时间设置成 UTC
#timedatectl set-local-rtc 1 # 硬件时间设置成 RTC (本地时间)(不建议！)

# 设置系统支持的编码
#en_US.UTF-8
#zh_CN.GB18030 GB18030
#zh_CN.GBK GBK
#zh_CN.UTF-8 UTF-8
#zh_CN GB2312
sed -e '/#en_US.UTF-8\|#zh_CN.GB18030\|#zh_CN.GBK\|#zh_CN GB2312\|#zh_CN.UTF-8\|#ja_JP.UTF-8 UTF-8\|#zh_TW BIG5\|#zh_TW.UTF-8/{s/#//}' -i /etc/locale.gen
    locale-gen
    echo 'LANG=en_US.UTF-8' > /etc/locale.conf
}

install_grub(){
    # 挂载ESP分区
    mkdir -p /boot/efi
    mount "$ESP" /boot/efi

    # 安装GRUB
    pacman -S --noconfirm --needed dosfstools e2fsprogs grub efibootmgr os-prober
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=arch_grub --recheck # UEFI方式安装
    # grub-install --recheck /dev/<目标磁盘> # BIOS方式安装
    grub-mkconfig -o /boot/grub/grub.cfg
}

install_driver(){
    # 安装显卡驱动
    nvidia=`(lspci | grep -E 'VGA|3D|Display' | grep 'NVIDIA' )`
    amd=`(lspci | grep -E 'VGA|3D|Display' | grep 'AMD' )`
    intel=`(lspci | grep -E 'VGA|3D|Display' | grep 'Intel' )`
    radeon=`(lspci | grep -E 'VGA|3D|Display' | grep 'Radeon')`

    if [[ -n "$nvidia" ]]; then
        pacman -S --noconfirm nvidia
    fi

    if [[ -n "$radeon" ]]; then
        pacman -S --noconfirm xf86-video-ati
    fi

    if [[ -n "$amd" ]]; then
        pacman -S --noconfirm xf86-video-amdgpu
    fi

    if [[ -n "$intel" ]]; then
        pacman -S --noconfirm xf86-video-intel
    fi

    if [[ -n "$nvidia" && -n "$intel" ]]; then
        pacman -S --noconfirm bumblebee
        gpasswd -a "$USERNAME" bumblebee
        systemctl enable bumblebeedg
    fi

    # 安装触摸板驱动
    if [[ -n "`grep 'Synaptics TouchPad' /proc/bus/input/devices`" ]]; then
        pacman -S --noconfirm xf86-input-synaptics
    fi
}

install_all(){
    # 安装软件与桌面环境
    pacman -S --noconfirm --needed "${DE_packages[@]}"
    pacman -S --noconfirm --needed "${APP_packages[@]}"
    #pacman -S --noconfirm --needed "${WM_packages[@]}"
    #pacman -R --noconfirm "${Remove_packages[@]}"
}

user_configure(){
    #echo -e "\e[1;46m - 设置root账户密码 - \e[0m"
    echo -e "\e[1;46m - Set Root Password - \e[0m"
    passwd root # 设置root账户密码
    #echo -e "\e[1;46m - 请设置新用户密码 - \e[0m"
    echo -e "\e[1;46m - Set NewUser Password - \e[0m"
    passwd "$USERNAME" # 设置新用户密码
    echo "system config finish!"
}

system_configure
install_all
install_driver
install_grub
user_configure

exit
